package com.meizhuo.listview;


import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;

public class MainActivity extends Activity implements View.OnClickListener {


    private MyFragment fg1;
    private MyFragment1 fg2;
    private MyFragment2 fg3;
    private MyFragment3 fg4;
    private FragmentManager fManager;
    private LinearLayout wechat;
    private LinearLayout find;
    private LinearLayout maillist;
    private LinearLayout me;
    private FragmentTransaction fTransaction;
    private TextView title;
    private LinearLayout header;
    private AppCompatImageView img1;
    private TextView tab1;
    private AppCompatImageView img2;
    private TextView tab2;
    private AppCompatImageView img3;
    private TextView tab3;
    private AppCompatImageView img4;
    private TextView tab4;
    private ImageView addFriend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();

        wechat = findViewById(R.id.wechat);
        find = findViewById(R.id.find);
        maillist = findViewById(R.id.maillist);
        me = findViewById(R.id.me);


        fManager = getFragmentManager();
        bindViews();
        wechat.performClick();   //模拟一次点击，既进去后选择第一项
        img1.setImageResource(R.drawable.wechat_selected);
        tab1.setTextColor(Color.BLUE);
    }

    private void bindViews() {

        wechat.setOnClickListener(this);
        find.setOnClickListener(this);
        maillist.setOnClickListener(this);
        me.setOnClickListener(this);
        Log.d("run start", "run ");
    }

    @Override
    public void onClick(View v) {
        fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (v.getId()) {
            case R.id.wechat:
                setSelected();
                wechat.setSelected(true);
                img1.setImageResource(R.drawable.wechat_selected);
                tab1.setTextColor(Color.BLUE);
                title.setText("微信");
                if (fg1 == null) {
                    fg1 = new MyFragment(getApplicationContext());
                    fTransaction.add(R.id.ly_content, fg1);
                } else {
                    fTransaction.show(fg1);
                }
                break;
            case R.id.maillist:
                Log.d("a", "aa");
                setSelected();
                maillist.setSelected(true);
                img2.setImageResource(R.drawable.maillist_selected);
                tab2.setTextColor(Color.BLUE);
                title.setText("通讯录");
                if (fg2 == null) {
                    fg2 = new MyFragment1(getApplicationContext());
                    fTransaction.add(R.id.ly_content, fg2);
                } else {

                    fTransaction.show(fg2);
                }
                break;
            case R.id.find:
                setSelected();
                find.setSelected(true);
                img3.setImageResource(R.drawable.find_selected);
                tab3.setTextColor(Color.BLUE);
                title.setText("发现");
                if (fg3 == null) {
                    fg3 = new MyFragment2(getApplicationContext());
                    fTransaction.add(R.id.ly_content, fg3);
                } else {
                    fTransaction.show(fg3);
                }
                break;
            case R.id.me:
                setSelected();
                me.setSelected(true);
                img4.setImageResource(R.drawable.me_selected);
                tab4.setTextColor(Color.BLUE);
                title.setText("我");
                if (fg4 == null) {
                    fg4 = new MyFragment3(getApplicationContext());
                    fTransaction.add(R.id.ly_content, fg4);
                } else {
                    fTransaction.show(fg4);
                }
                break;
            case R.id.addFriend:
                Intent intent = new Intent(MainActivity.this,AddFriendActivity.class);
                startActivity(intent);
                break;

        }
        fTransaction.commit();
    }

    private void setSelected() {

        wechat.setSelected(false);
        find.setSelected(false);
        maillist.setSelected(false);
        me.setSelected(false);
        tab1.setTextColor(Color.BLACK);
        tab2.setTextColor(Color.BLACK);
        tab3.setTextColor(Color.BLACK);
        tab4.setTextColor(Color.BLACK);
        img1.setImageResource(R.drawable.wechat);
        img2.setImageResource(R.drawable.maillist);
        img3.setImageResource(R.drawable.find);
        img4.setImageResource(R.drawable.me);

    }

    private void hideAllFragment(FragmentTransaction fTransaction) {

        if (fg1 != null) fTransaction.hide(fg1);
        if (fg2 != null) fTransaction.hide(fg2);
        if (fg3 != null) fTransaction.hide(fg3);
        if (fg4 != null) fTransaction.hide(fg4);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        header = (LinearLayout) findViewById(R.id.header);
        img1 = (AppCompatImageView) findViewById(R.id.img1);
        tab1 = (TextView) findViewById(R.id.tab1);
        img2 = (AppCompatImageView) findViewById(R.id.img2);
        tab2 = (TextView) findViewById(R.id.tab2);
        img3 = (AppCompatImageView) findViewById(R.id.img3);
        tab3 = (TextView) findViewById(R.id.tab3);
        img4 = (AppCompatImageView) findViewById(R.id.img4);
        tab4 = (TextView) findViewById(R.id.tab4);
        addFriend = (ImageView) findViewById(R.id.addFriend);
        addFriend.setOnClickListener(this);
    }
}
